package com.power.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.IndexImg;
import com.power.model.Result;
import com.power.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 轮播图业务控制层
 * @Author dingchunlin
 * @Date 2024/07/17 21:37
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/indexImg")
@Api(tags = "轮播图接口管理")
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;

    /**
     * 多条件分页查询轮播图
     * @param current 页码
     * @param size 每页条数
     * @param status 状态
     * @return 分页数据
     */
    @ApiOperation(value = "多条件分页查询轮播图")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> loadIndexImgPage(@RequestParam Long current,
                                                    @RequestParam Long size,
                                                    @RequestParam(required = false) Integer status) {
        // 创建分页对象
        Page<IndexImg> page = new Page<>(current, size);
        // 查询条件
        LambdaQueryWrapper<IndexImg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotNull(status), IndexImg::getStatus, status);
        lambdaQueryWrapper.orderByDesc(IndexImg::getSeq);

        // 查询分页数据
        page = indexImgService.page(page, lambdaQueryWrapper);
        // 返回结果
        return Result.success(page);
    }

    /**
     * 新增轮播图
     * @param indexImg 轮播图实体
     * @return 操作结果
     */
    @ApiOperation(value = "新增轮播图")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('admin:indexImg:save')")
    public Result<String> saveIndexImg(@RequestBody IndexImg indexImg) {
        Boolean saved = indexImgService.saveIndexImg(indexImg);
        return Result.handle(saved);
    }

    /**
     * 根据标识查询轮播图
     * @param id 轮播图标识
     * @return 轮播图实体
     */
    @ApiOperation(value = "根据标识查询轮播图")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAnyAuthority('admin:indexImg:info')")
    public Result<IndexImg> loadIndexImgInfo(@PathVariable("id") Long id) {
        IndexImg indexImg = indexImgService.loadIndexImgInfo(id);
        return Result.success(indexImg);
    }

    /**
     * 修改轮播图
     * @param indexImg 轮播图实体
     * @return 操作结果
     */
    @ApiOperation(value = "修改轮播图")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('admin:indexImg:update')")
    public Result<String> modifyIndexImg(@RequestBody IndexImg indexImg) {
        Boolean modified = indexImgService.modifyIndexImg(indexImg);
        return Result.handle(modified);
    }

    /**
     * 批量删除轮播图
     * @param imageIds 轮播图标识数组
     * @return 操作结果
     */
    @ApiOperation(value = "批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('admin:indexImg:delete')")
    public Result<String> removeIndexImg(@RequestBody List<Long> imageIds) {
        Boolean removed = indexImgService.removeIndexImgByImageIds(imageIds);
        return Result.handle(removed);
    }


    /**
     * 查询小程序轮播图列表
     * @return 轮播图列表
     */
    @ApiOperation(value = "查询小程序轮播图列表")
    @GetMapping("indexImgs")
    public Result<List<IndexImg>> loadWxIndexImgsList() {
        List<IndexImg> indexImgList = indexImgService.queryWxIndexImgsList();
        return Result.success(indexImgList);
    }

}
